package com.hs.sfzy.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hs.sfzy.entity.AppointmentDTO;
import com.hs.sfzy.entity.Consult;
import com.hs.sfzy.entity.ConsultLevel;
import com.hs.sfzy.entity.ConsultSpecialty;
import com.hs.sfzy.entity.Specialty;
import com.hs.sfzy.mapper.ConsultLeverMapper;
import com.hs.sfzy.mapper.ConsultMapper;
import com.hs.sfzy.mapper.SpecialtyMapper;
import com.hs.sfzy.service.ConsultService;
import com.hs.sfzy.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

/**
 * @ClassName ConsultServiceImpl
 * @Description TODO
 * @Author 李明辉
 * @Date 2024年10月24日 11:05
 * @Version 1.0
 */
@Service
public class ConsultServiceImpl implements ConsultService {
@Autowired
private ConsultMapper consultMapper;
@Autowired
private SpecialtyMapper specialtyMapper;
@Autowired
private ConsultLeverMapper consultLeverMapper;

    @Override
    public int insert(Consult consult) {
        return  consultMapper.insert(consult);
    }

    @Override
    public List<Consult> findAll(Consult consult) {
        return consultMapper.select(consult);
    }

    @Override
    public PageInfo<Consult> selectByPageInfo(int pageNum, int pageSize, Consult consult) {
        PageHelper.startPage(pageNum, pageSize);
        List<Consult> consults = consultMapper.select(consult);
        consults.forEach(con -> {
            List<ConsultSpecialty> consultSpecialties = con.getConsultSpecialties();
            consultSpecialties.forEach(consultSpecialty -> {
                Long specialtyId = consultSpecialty.getSpecialtyId();
                Specialty specialty = specialtyMapper.selectById(specialtyId);
                consultSpecialty.setSpecialty(specialty);
            });
        });
        PageInfo<Consult> consultPageInfo = new PageInfo<Consult>(consults);
        return consultPageInfo;
    }

    @Override
    public int update(Consult consult) {
//        long id = consult.getSpecialty().getId();
//        Specialty specialty = specialtyMapper.selectById(id);

        return consultMapper.update(consult) ;
    }

    @Override
    public int delete(Long id) {
        List<Consult> select = consultMapper.select(new Consult());
        if (select.size()>0){
            return Result.error("删除失败").getStatus();
        }
        return consultMapper.delete(id);
    }

    /**
     * 根据id查询咨询师
     * @author QinKunPeng
     * @date 2024/10/24 19:56
     * @param id
     * @return com.hs.sfzy.entity.Consult
     */
    @Override
    public Consult getById(Long id) {
        return consultMapper.selectById(id);
    }

    @Override
    public List<Consult> getIdAndName() {
        return consultMapper.getIdAndName();
    }

    @Override
    public Consult selectSimpleById(Long id) {
        Consult consult = consultMapper.selectSimpleById(id);
        // 获取咨询师等级
        Long levelId = consult.getLevelId();
        ConsultLevel consultLevel = consultLeverMapper.selectById(levelId);
        consult.setConsultLevel(consultLevel);
        // 获取咨询师擅长领域集合
        List<ConsultSpecialty> consultSpecialties = consult.getConsultSpecialties();
        for (ConsultSpecialty consultSpecialty : consultSpecialties) {
            Long specialtyId = consultSpecialty.getSpecialtyId();
            Specialty specialty = specialtyMapper.selectById(specialtyId);
            consultSpecialty.setSpecialty(specialty);
        }
        return  consult;
    }

    @Override
    public List<Consult> findByInfo(AppointmentDTO appointmentDTO) {
//        System.out.println(appointmentDTO.getSpecialties().toString());
        if(appointmentDTO.getSpecialties().get(0).equals("[]")){
            appointmentDTO.setSpecialties(null);
//            System.out.println("lll");
        }else{
            String s = appointmentDTO.getSpecialties().toString();
            System.out.println(s);
            s = s.replace("[","");
            System.out.println(s);
            s = s.replace("]","");
            System.out.println(s);
            s = s.replace("\"","");
            System.out.println(s);
            String[] split = s.split(",");
            System.out.println(split);
            appointmentDTO.setSpecialties(Arrays.asList(split));
        }
        System.out.println("appointmentDTO = " + appointmentDTO);
        return consultMapper.findByInfo(appointmentDTO);
    }

}
